﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;

namespace DnPakExplorer {
	public interface ICharacterProperty {

		double GetProperty(PropertyID pid);

		float critical { get; }			// 致命概率
		float criticalDamage { get; }	// 致命伤害

		float finalDamage { get; }		// 最终伤害

		float physicalDefense { get; }	// 物理防御力
		float magicDefense { get; }		// 魔法防御力

		float fireAttack { get; }		// 火攻击
		float waterAttack { get; }		// 水攻击
		float lightAttack { get; }		// 光攻击
		float darkAttack { get; }		// 暗攻击

		int hp { get; }					// hp
		int mp { get; }					// mp

		int strength { get; }			// 力量
		int agility { get; }			// 敏捷
		int intelligence { get; }		// 智力
		int stamina { get; }			// 体质

		int minPhysicalAttack { get; }	// 最小物理攻击
		int maxPhysicalAttack { get; }	// 最大物理攻击
		int minMagicAttack { get; }		// 最小魔法攻击
		int maxMagicAttack { get; }		// 最大魔法攻击

		float criticalPoint { get; }	// 致命点数
		float criticalDamagePoint { get; }	// 致命伤害点数

		int finalDamagePoint { get; }	// 最终伤害点数

		int physicalDefensePoint { get; }	// 物理防御力点数
		int magicDefensePoint { get; }		// 魔法防御力点数

	}

	interface IEquipmentPanel {
		int GetPointProperty(PropertyID pid);
		float GetFloatProperty(PropertyID pid);
	}

	class EquipmentPanel : IEquipmentPanel {

		public List<Item> mItems = new List<Item>();

		public EquipmentPanel() {
			
		}

		int[] mPointProperty = new int[(int)PropertyID.ID_MAX];
		float[] mFloatProperty = new float[(int)PropertyID.ID_MAX];

		public int GetPointProperty(PropertyID pid) {
			return mPointProperty[(int)pid];
		}

		public float GetFloatProperty(PropertyID pid) {
			return mFloatProperty[(int)pid];
		}

		void AddItem(Item item) {
			mItems.Add(item);
			RecalculateProperty();
		}

		void RemoveItem(Item item) {
			mItems.Remove(item);
			RecalculateProperty();
		}

		/// <summary>
		/// 重新计算属性值到缓存
		/// </summary>
		void RecalculateProperty() {
			for (int i=0; i<mPointProperty.Length; i++)
				mPointProperty[i] = 0;
			for (int i=0; i<mPointProperty.Length; i++)
				mFloatProperty[i] = 0;
			foreach (Item item in mItems) {
				//foreach(Property prop in item.mPropertyList){
				//	mPointProperty[(int)prop.id] += prop.pointValue;
				//	mFloatProperty[(int)prop.id] += prop.floatValue;
				//}
			}
		}
	}

	public class Property {
		public object tag;
		public PropertyID id;
		public int type;
		public int pointValue;
		public float floatValue;
	}

	//public class Item {
	//	public List<Property> mPropertyList = new List<Property>();
	//	public Dntable fromTable;		// 所在表
	//	public int id;					// 物品ID

	//	// Fixed Field

	//	public string name;				// 名称
	//	public int limitLevel;			// 等级限制
	//	public int rank;				// 级别
	//	public string needJobClass;		// 职业类型
	//	public int iconId = -1;			// 图标索引
	//	public string description;		// 描述

	//	public int exchangType = -1;		// 交易(物品)类型
	//	public int typeParam1 = -1;		// 制作属性类型
	//	public int typeParam2 = -1;		// 附魔属性类型
	//	public int enchantId = -1;		// 强化属性表
	//	public int slotsType = -1;		// 龙玉槽类型
	//	public int setId = -1;			// 套装ID
	//	public int imprintType = -1;		// 刻印类型
	//	public int skillId = -1;			// 技能ID
	//	public int skillDecription;

	//	public bool hasFullProperty;
	//	public SetEntry set;

	//	// User Field

	//	public int appliedType1 = -1;				// 应用的制作属性
	//	public int appliedType2 = -1;				// 应用的附魔属性
	//	public int appliedEnchantLevel = -1;			// 应用的强化等级
	//	public int imprintId = -1;					// 应用的刻印ID
	//	public List<DragonJewelSlot> slotsList;		// 镶嵌的龙玉
		
	//}

	public struct DragonJewelSlot {
		public int type;
		public Item item;

		public DragonJewelSlot(int type) {
			this.item = null;
			this.type = type;
		}
	}

	public class SetEntry {
		public static Regex mStateVlauePettern = new Regex(@"\d*\.?\d+$");

		public int setID;
		public List<string> itemNames;
		public List<Property> properties {
			get {
				if (_properties == null) {
					_properties = new List<Property>();
					foreach (Dntable table in DnResourceUtil.SetTables) {
						for (int r=0; r<table.RecordCount; r++) {
							if (table.GetKey(r) == setID) {
								for (int i=0; i<=33; i++) {
									if ((int)table[r, 0] <=0 /*|| (int)table[r+1, 0]<0*/)
										break;
									Property prop = new Property();
									prop.id = (PropertyID)((int)table[r, i++]*1000+(int)table[r, i++]);
									string value = (string)table[r, i];
									if (mStateVlauePettern.IsMatch(value)) {
										prop.floatValue = float.Parse(value);
										prop.pointValue = (int)prop.floatValue;
									} else {
										prop.floatValue = float.NaN;
										break;
									}
									_properties.Add(prop);
								}
								break;
							}
						}
					}
				}
				return _properties;
			}
		}
		private List<Property> _properties;
		public string setDescription;
	}

	public enum PropertyID : int{
			Strength			=0,			// 力量
			Agility				=1,			// 敏捷
			Intelligence		=2,			// 智力
			Stamina				=3,			// 体质

			MinPysicalAttack	=4,
			MaxPysicalAttack	=5,	
			MinMagicAttack		=6,
			MaxMagicAttack		=7,

			PysicalDefense		=8,
			MagicDefense		=9,

			Critical			=12,
			CriticalDamage		=103,
			FinalDamage			=29,

			FireAttack			=16,
			WaterAttack			=17,
			LightAttack			=18,
			DarkAttack			=19,
			
			HP					=25,
			MP					=26,
			
			ID_MAX				=200,
		}

	public enum ItemType : int{
		Weapon1 = 1,
		Weapon2 = 2,
		Head = 3,
		Body = 4,
		Leg = 5,
		Glove = 6,
		Boots = 7,
		Necklace = 8,
		Earing = 9,
		Ring = 10,
		Glyph = 33,		// 纹章
		SkillGlyph = 34,
		SpecialSkillGlypgh = 35,
		DragonJewel = 54,	// 龙玉
		SkillDragonJewel = 55,
		Tailsman = 52,	// 护符
		EquipmentMaterial = 66,
		DraonJewelMaterial = 67,
		TailsmanMaterial = 68,
		SkillJewelryMaterial = 69,
		Source = 51,	// 根源
		Gem = 12,		// 宝玉
		Coupon = 64,	// 交换券
		SealItem = 65,	// 封印印章
		DrugAndSroll = 11,	// 药水/卷轴
		Mercenary = 74,		// 佣兵
		MonsterCard = 75,
		Other = 15,
		Weapon1Cash = 16,
		Weapon2Cash = 17,
		HeadCash = 18,
		BodyCash = 19,
		LegCash = 20,
		GloveCash = 21,
		BootsCash = 22,
		NecklaceCash = 23,
		EaringCash = 24,
		RingCash = 25,
		WingCash = 26,
		TailCash = 27,
		DrugAndScrollCash = 30,
		EthantCash = 31,
		ChangeFaceCash = 48,
		DragonMoneyCash = 49,
		OtherCash = 32,
		CookBook = 39,
		Food = 40,
		Seed = 41,
		FarmingProduct = 42,
		FishProduct = 43,
	}

	
}
